package com.owen.springboot.smartparkingms.module.app.controller;


import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.ExcelExport;
import com.owen.springboot.smartparkingms.module.app.entity.CarOrder;
import com.owen.springboot.smartparkingms.module.app.service.ICarOrderService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/app/carOrder")
public class CarOrderController {

    @Autowired
    private ICarOrderService carOrderService;

    /**
     * 列表
     */
    @PostMapping("list")
    public Result list(CarOrder carOrder){
        return carOrderService.listPage(carOrder);
    }

    /**
     * 导出
     */
    @PostMapping("export")
    @RequiresRoles(value={"admin"},logical = Logical.OR)
    public void export(Long parkId, HttpServletRequest request, HttpServletResponse response){
        try{
            ExcelExport excelExport = carOrderService.exportData(parkId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            excelExport.writeTemplate(response, request,
                    "车场订单信息-" + sdf.format(new Date()) + ".xls");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
